package com.vegetable.modules.controller.finance;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.finance.PaymentPurchase;
import com.vegetable.modules.entity.finance.po.AgentDeliveryStatisticsPO;
import com.vegetable.modules.entity.finance.po.PurchaseDeliveryStatisticsPO;
import com.vegetable.modules.entity.finance.vo.PaymentAgentStatisticsVO;
import com.vegetable.modules.service.finance.IPaymentPurchaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * <p>
 * 采购/代办付款 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-04-15
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/payment/agent")
@Api(value = "采购/代办付款", tags = "采购/代办付款")
public class PaymentAgentController extends AbstractController {

    private final IPaymentPurchaseService paymentService;

    /**
     * 采购/代办付款列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "采购/代办付款列表", notes = "采购/代办付款列表")
    public R list(@RequestParam Map<String, Object> params) {
        log.info("采购/代办付款列表：{}", params);
        PageUtils page = paymentService.queryPage(params);
        return R.data(page);
    }

    /**
     * 根据id查询详情待办付款
     */
    @GetMapping("/detailPurchase")
    @ApiOperation(value = "根据id查询详情待办付款", notes = "根据id查询详情待办付款")
    public R detailPurchase(Long id) {
        if (id == null) {
            return R.fail("订单主键不能为空");
        }
        log.info("根据id查询详情待办付款：{}", id);
        return paymentService.detailPurchase(id);
    }

    /**
     * 收款确定
     */
    @SysLog(value = "收款确定")
    @PostMapping("/completePayment")
    @ApiOperation(value = "收款确定", notes = "收款确定")
    public R completePayment(@RequestBody PaymentPurchase payment) {
        log.info("收款确定：{}", payment);
        if (payment.getId() == null) {
            return R.fail("订单id主键不能为空");
        }
        payment.setOrderType(1);
        payment.setState(1);
        try {
            return paymentService.savePaymentAgent(payment);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除代办付款
     */
    @SysLog(value = "删除代办付款")
    @GetMapping("/deletePayment")
    @ApiOperation(value = "根据id删除代办付款", notes = "根据id删除代办付款")
    public R deletePayment(Long id) {
        log.info("删除代办付款：{}", id);
        try {
            return paymentService.deletePayment(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 代办付款统计
     */
    @SysLog(value = "代办付款统计")
    @PostMapping("/statisticsAgentDeliver")
    @ApiOperation(value = "代办付款统计", notes = "代办付款统计")
    public R agentDeliveryStatistics(@RequestBody AgentDeliveryStatisticsPO po) {
        log.info("代办付款统计：{}", po);
        return paymentService.agentDeliveryStatistics(po);
    }

    @SysLog(value = "采购付款情况统计")
    @PostMapping("/statisticsPurchaseDeliver")
    @ApiOperation(value = "代办交货付款情况统计", notes = "代办交货付款情况统计")
    public R purchaseDeliveryStatistics(@RequestBody PurchaseDeliveryStatisticsPO po){
        return paymentService.purchaseDeliveryStatistics(po);
    }

    /**
     * 新增代办付款
     * 根据id查询详情待办付款
     */
    @SysLog(value = "新增代办付款")
    @PostMapping("/savePayment")
    @ApiOperation(value = "新增代办付款", notes = "新增代办付款")
    public R savePayment(@RequestBody PaymentPurchase payment){
        log.info("新增代办付款：{}", JSON.toJSONString(payment));
        payment.setCreateUserId(getUserId());
        payment.setOrderType(1);
        try {
            return paymentService.savePaymentAgent(payment);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }
}